package net.alan.modules.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * 捕获异常错误信息
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@ControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Throwable.class)
    public ModelAndView noMapping(Throwable e, WebRequest request) {
        ModelAndView view = new ModelAndView();
        Throwable sube = e.getCause();
        if(sube instanceof  BaseExecption){
            view.setViewName("error/error");
        }else if(sube instanceof LoginExecption){
            view.setViewName("web/login");
        }
        String msg = sube.getMessage();
        view.addObject("e", msg);
        return view;
    }

}
